package com.lcq.gulimall.ware.listener;

import com.lcq.common.to.OrderTo;
import com.lcq.common.to.StockLockedTo;
import com.lcq.gulimall.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@RabbitListener(queues = "stock-release-queue")
@Slf4j
public class StockReleaseQueueListener {
    @Autowired
    WareSkuService wareSkuService;

    @RabbitHandler
    public void orderReleaseQueueConsumer(StockLockedTo stockLockedTo, Channel channel, Message message) throws IOException {
        log.info(">>>>>>>>>>>>>>>库存工作单回溯>>>>>>>>>>>>>");
        try {
            wareSkuService.releaseLockWare(stockLockedTo);
            log.info(">>>>>>>>>>>>>>>库存工作单回溯完成>>>>>>>>>>>>>");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            e.printStackTrace();
            log.info(">>>>>>>>>>>>>>>库存工作单回溯失败>>>>>>>>>>>>>");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

    @RabbitHandler
    public void orderReleaseQueueConsumer(OrderTo orderTo, Channel channel, Message message) throws IOException {
        log.info(">>>>>>>>>>>>>>>库存工作单回溯>>>>>>>>>>>>>");
        try {
            wareSkuService.releaseLockWare(orderTo);
            log.info(">>>>>>>>>>>>>>>库存工作单回溯完成>>>>>>>>>>>>>");
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            e.printStackTrace();
            log.info(">>>>>>>>>>>>>>>库存工作单回溯失败>>>>>>>>>>>>>");
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
